package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.RegionInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.RegionInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.RegionInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author LiuYuLiang.LL
 */
@Service
public class RegionInfoServiceImpl extends ServiceImpl<RegionInfoMapper, RegionInfo> implements RegionInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public IPage<RegionInfo> findByPage(QueryRequest request, RegionInfo info) {
        try {
            LambdaQueryWrapper<RegionInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getRegionCode())) {
                queryWrapper.like(RegionInfo::getRegionCode, info.getRegionCode());
            }
            if (StringUtils.isNotBlank(info.getRegionName())) {
                queryWrapper.like(RegionInfo::getRegionName, info.getRegionName());
            }
            if (StringUtils.isNotBlank(info.getParentCode())) {
                queryWrapper.like(RegionInfo::getParentCode, info.getParentCode());
            }
            queryWrapper.orderByDesc(RegionInfo::getCreateTime);
            Page<RegionInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    public void create(RegionInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setStatus(BusinessContainer.data_sta_01);
        entity.setAuditStatus(BusinessContainer.status_02);
        entity.setOprId(user.getId());
        entity.setCrtOrgId(user.getOrgCode());
        entity.setCreateTime(lastUpddt);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);

        this.baseMapper.insert(entity);
    }

    @Override
    public void update(RegionInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        // 修改
        entity.setAuditStatus(BusinessContainer.status_42);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void cancel(String id) {
        // 注销
        RegionInfo entity = this.getById(id);
        entity.setStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    @Transactional
    public RegionInfo findOneByCode(String regionCode, String status) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionCode, regionCode);
        wrapper.eq(RegionInfo::getStatus, status);
        return this.getOne(wrapper);
    }

    @Override
    public RegionInfo findOneByCode(String regionCode) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionCode, regionCode);
        return this.getOne(wrapper);
    }

    @Override
    public RegionInfo findByUkAndStatusAndPtCode(String regionCode, String status, String parentCode) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionCode, regionCode);
        wrapper.eq(RegionInfo::getStatus, status);
        wrapper.eq(RegionInfo::getParentCode, parentCode);
        return this.getOne(wrapper);
    }

    @Override
    public List<RegionInfo> getByName(String regionName) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionName, regionName);
        wrapper.eq(RegionInfo::getStatus, "01");
        return this.list(wrapper);
    }

    @Override
    public List<Map> provinceAreaInfoSelect() {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionLevel, "01");
        wrapper.eq(RegionInfo::getStatus, "01");
        List<RegionInfo> list1 = this.list(wrapper);
        List<Map> list = new ArrayList<>();
        Map<String, Object> info;
        for (RegionInfo regionInfo : list1) {
            info = new HashMap<>();
            info.put("PROVINCE_CODE", regionInfo.getRegionCode());
            info.put("AREA_NAME", regionInfo.getRegionName());
            list.add(info);
        }
        return list;
    }

    @Override
    public Map<String, List<String>> getAREA_CD(List listPro) {
        Map<String, List<String>> siteMap = new HashMap<String, List<String>>();
        if (listPro.size() > 0) {
            for (int p = 0; p < listPro.size(); p++) {//遍历省
                Map mapPro = (Map) listPro.get(p);
                String areaInfPro = (String) mapPro.get("AREA_NAME");
                String areaCdPro = (String) mapPro.get("PROVINCE_CODE");
                List listCity = getXjCity(areaCdPro);
                List listC = getXjCity(listCity);
                if (listCity.size() > 0) {
                    Map<String, String> map = new HashMap<String, String>();
                    for (int c = 0; c < listCity.size(); c++) {//遍历市
                        Map mapCity = (Map) listCity.get(c);
                        String areaCdCity = (String) mapCity.get("CITY_CODE");
                        String areainfcity = (String) mapCity.get("AREA_NAME");
                        List<String> listCout = getXjCountry(areaCdCity);
                        siteMap.put(areainfcity, listCout);
                    }
                }

                siteMap.put(areaInfPro, listC);
            }
        }

        return siteMap;
    }

    /*根据省获取市*/
    public List getXjCity(String areaCd) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionLevel, "02");
        wrapper.eq(RegionInfo::getStatus, "01");
        wrapper.eq(RegionInfo::getParentCode, areaCd);
        List<RegionInfo> list1 = this.list(wrapper);
        List<Map> list = new ArrayList<>();
        Map<String, Object> info;
        for (RegionInfo regionInfo : list1) {
            info = new HashMap<>();
            info.put("CITY_CODE", regionInfo.getRegionCode());
            info.put("AREA_NAME", regionInfo.getRegionName());
            list.add(info);
        }
        return list;
    }

    public List getXjCity(List cityList) {
        List<String> cityLst = new ArrayList<String>();
        if (cityList.size() > 0) {
            for (int i = 0; i < cityList.size(); i++) {
                String areaInf = (String) ((Map) cityList.get(i)).get("AREA_NAME");
                cityLst.add(areaInf);
            }
        }
        return cityLst;
    }

    /*根据市获取区县*/
    public List<String> getXjCountry(String areaCd) {
        return this.baseMapper.getXjCountry(areaCd);
    }


    /**
     * 获取省市区
     * makeby   --lizk
     *
     * @param q  regionCode
     * @param p  parentCode
     * @param l  级别
     * @return
     */
    @Override
    public RegionInfo findPrncCityArea(String q, String p, String l) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        if ("01".equals(l)) {
            wrapper.eq(RegionInfo::getRegionLevel, l);
            wrapper.eq(RegionInfo::getRegionCode, q);
        } else {
            wrapper.eq(RegionInfo::getRegionLevel, l);
            wrapper.eq(RegionInfo::getRegionCode, q);
            wrapper.eq(RegionInfo::getParentCode, p);
        }
        return baseMapper.selectOne(wrapper);
    }


    @Override
    public List<RegionInfo> findProvince(String regionLevel) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionLevel, regionLevel);
        return this.list(wrapper);
    }

    @Override
    public List<RegionInfo> findCityOrArea(String cityOrAreaCode, String regionLevel) {
        LambdaQueryWrapper<RegionInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionInfo::getRegionLevel, regionLevel);
        wrapper.eq(RegionInfo::getParentCode, cityOrAreaCode);
        return this.list(wrapper);
    }
}
